// 坐标取地址
function getAddressName(lng, lat) {
  var dfd = $.Deferred()
  AMap.service('AMap.Geocoder', function () {
    geocoder = new AMap.Geocoder({
      city: "010"//城市，默认：“全国”
    });
    var lnglatXY = [lng, lat];//关联的坐标
    geocoder.getAddress(lnglatXY, function (status, result) {
      if (status === 'complete' && result.info === 'OK') {
        dfd.resolve(result.regeocode.formattedAddress);
      } else {
        dfd.resolve('未找到有效地址');
      }
    });
  })

 return dfd.promise();
}


(function (window, $, undefined) {
  'use strict';

  var cityLists = [];
  var cityData = {};

  var curCityId = '';

  var dictData = {
    autoBrandId: {},
    type: {},
    vehicleColor: {},
    state: {},
    auto: {}
  }

  main(function () {

    // 获取所有城市列表
    $.each(ADDR_DATA.childs, function (_, item) {
      if (item.childs.length <= 1) {
        cityLists.push({
          id: (item.childs[0] && item.childs[0].id) || item.id,
          name: item.name
        });
      } else {
        // TODO
        // 县级市 ...
        // ---#

        $.each(item.childs, function (x, city) {
          cityLists.push({
            id: city.id,
            name: city.name[0] === '省' ? item.name + city.name : city.name
          });
        });
      }
    });

    // 后缀去除
    $.each(cityLists, function (_, item) {
      item.name = item.name.replace(/[省市]$/, '');
    });

    var $layerDispatch = $('#layer-dispatch');
    var $cityList = $('.citylist>.row');

    $cityList.on('click', '.city-item', function () {
      var $this = $(this);
      var id = $this.data('cid');
      var name = $this.parent().text();
      openDispatch(id, name);
    });

    // 获取城市列表
    getCityList();
    function getCityList(callback) {

      util.ajax(ajaxURL.ORGANIZATION + '/city/getCityList?pageNum=1&pageSize=99999', '', 'GET', util.resCallback(function (res) {
        var ret = '';
        cityData = {};
        $.each(res.data, function (_, item) {
          cityData[item.cityId] = item;
          ret += '<div class="col-xs-3"><div class="city-item" data-cid="' + item.cityId + '" data-kid="' + item.kid + '"><div class="city-name">' + item.name + '</div><div class="city-del"></div></div></div>';
        });
        $cityList.find('.col-xs-3').not('.first').remove();
        $cityList.prepend(ret);

        if ($.isFunction(callback)) {
          callback();
        }
      }));
    }

    // 显示城市调度列表
    function openDispatch(id, name) {
      curCityId = id;
      $layerDispatch.find(':input').removeAttr('disabled');
      loadOptionallyBook(id);
      loadOptionallyNow(id);
      loadSpeciallyNow(id);
      loadSpeciallyBook(id);
      loadDriverTable(curCityId);
      setTab(1, 5);
      setSubTab(1, 4);
      util.layerOpen({
        title: '调度列表&emsp;城市：' + name,
        content: $layerDispatch,
        area: ['1200px', '700px']
      })
    };

    // subContent1
    //*********************************************************************************************************************/
    var $formOptionallyBook = $('#subContent1');
    var optionallyBookRounds = '';

    // 预约用车基础设置界面数据填充
    function loadOptionallyBook(id) {
      util.resetForm($formOptionallyBook);
      util.ajax(ajaxURL.SCHEDULE + '/v1/schedule/settings/getOptionallyBook', {
        city_id: id
      }, 'GET', util.resCallback(function (res) {

        // 获取派单轮次数
        optionallyBookRounds = res.data.rounds.length;

        var data = $.extend({}, res.data, { rounds1: res.data.rounds.length, interval1: res.data.rounds[0] && res.data.rounds[0].interval })
        util.fillForm($formOptionallyBook, data);

        // 填充派单轮次数据
        loadRoundsInfo(optionallyBookRounds, res.data.rounds, 'roundsInfo1');
      }
      ));
    }

    // 页面创建派单轮次设置
    $formOptionallyBook.find("input[name=rounds1]").change(function () {
      optionallyBookRounds = $(this).val();
      loadRoundsInfo(optionallyBookRounds, null, 'roundsInfo1');
    });

    // 验证表单
    var $validateOptionallyBook = $formOptionallyBook.validate({
      rules: {
        notify_time: { required: true, digits: true },
        min_time: { required: true, digits: true },
        max_time: { required: true, digits: true },
        is_send_msg: { required: true },
        msg_id: { required: true, stringCheck: true },
        range1: { required: true, digits: true },
        range2: { required: true, digits: true },
        range3: { required: true, digits: true },
        range4: { required: true, digits: true },
        range5: { required: true, digits: true },
        interval1: { required: true, digits: true },
        rounds1: { required: true, digits: true, max: 5, maxlength: 1 }
      },
      messages: {
        notify_time: { required: '请输入司机信息推送设置', digits: '请输入实数' },
        min_time: { required: '请输入可预约最短时间设置', digits: '请输入实数' },
        max_time: { required: '请输入可预约最长时间设置', digits: '请输入实数' },
        is_send_msg: { required: '请选择是否发短信给乘客' },
        msg_id: { required: '请输入短信模板ID' },
        range1: { required: '请输入筛选距离', digits: '请输入实数' },
        range2: { required: '请输入筛选距离', digits: '请输入实数' },
        range3: { required: '请输入筛选距离', digits: '请输入实数' },
        range4: { required: '请输入筛选距离', digits: '请输入实数' },
        range5: { required: '请输入筛选距离', digits: '请输入实数' },
        interval1: { required: '请输入间隔时间', digits: '请输入实数' },
        rounds1: { required: '请输入派单轮次', digits: '请输入整数', max: '请输入小于等于5的数字', maxlength: '长度不超过1' }
      },
      errorPlacement: function (error, element) { //错误信息位置设置方法
        error.appendTo(element.parents('.form-container')); //这里的element是录入数据的对象
      }
    });

    // 修改预约用车设置
    $('#optionallyBook-save').click(function () {
      // if(util.checkForm($formVehicle)) {
      var roundsData = [];

      getRoundsData($formOptionallyBook, 'roundsInfo1', roundsData);

      var data = util.formData($formOptionallyBook);
      data.rounds = roundsData;

      if ($validateOptionallyBook.form()) {
        var url = ajaxURL.SCHEDULE + '/v1/schedule/settings/updateOptionallyBook?city_id=' + curCityId;
        util.ajax(url, JSON.stringify(data), 'POST', util.resCallback(function (res) {
          layer.closeAll();
          util.tip.success('操作成功');
        }));
        // }
        // }else {
        //   util.tip.warn('请将数据填写完整');
        // }
      }
    });

    // subContent2
    //*********************************************************************************************************************/
    var $formOptionallyNow = $('#subContent2');
    var optionallyNowRounds = '';

    // 预约用车基础设置界面数据填充
    function loadOptionallyNow(id) {
      util.resetForm($formOptionallyNow);
      util.get(ajaxURL.SCHEDULE + '/v1/schedule/settings/getOptionallyNow', {
        city_id: id
      }, function (res) {

        // 获取派单轮次数
          optionallyNowRounds = res.data.rounds.length;

        var data = $.extend({}, res.data, { rounds2: res.data.rounds.length, interval2: res.data.rounds[0] && res.data.rounds[0].interval })
          util.fillForm($formOptionallyNow, data);

        // 填充派单轮次数据
          loadRoundsInfo(optionallyNowRounds, res.data.rounds, 'roundsInfo2');
      });
    }

    // 页面创建派单轮次设置
    $formOptionallyNow.find("input[name=rounds2]").change(function () {
      optionallyNowRounds = $(this).val();
      loadRoundsInfo(optionallyNowRounds, null, 'roundsInfo2');
    });

    // 验证表单
    var $validateOptionallyNow = $formOptionallyNow.validate({
      rules: {
        notify_time: { required: true, digits: true },
        min_time: { required: true, digits: true },
        max_time: { required: true, digits: true },
        is_send_msg: { required: true },
        msg_id: { required: true, stringCheck: true },
        range1: { required: true, digits: true },
        range2: { required: true, digits: true },
        range3: { required: true, digits: true },
        range4: { required: true, digits: true },
        range5: { required: true, digits: true },
        interval2: { required: true, digits: true },
        rounds2: { required: true, digits: true, max: 5, maxlength: 1 }
      },
      messages: {
        notify_time: { required: '请输入司机信息推送设置', digits: '请输入实数' },
        min_time: { required: '请输入可预约最短时间设置', digits: '请输入实数' },
        max_time: { required: '请输入可预约最长时间设置', digits: '请输入实数' },
        is_send_msg: { required: '请选择是否发短信给乘客' },
        msg_id: { required: '请输入短信模板ID' },
        range1: { required: '请输入筛选距离', digits: '请输入实数' },
        range2: { required: '请输入筛选距离', digits: '请输入实数' },
        range3: { required: '请输入筛选距离', digits: '请输入实数' },
        range4: { required: '请输入筛选距离', digits: '请输入实数' },
        range5: { required: '请输入筛选距离', digits: '请输入实数' },
        interval2: { required: '请输入间隔时间', digits: '请输入实数' },
        rounds2: { required: '请输入派单轮次', digits: '请输入整数', max: '请输入小于等于5的数字', maxlength: '长度不超过1' }
      },
      errorPlacement: function (error, element) { //错误信息位置设置方法
        error.appendTo(element.parents('.form-container')); //这里的element是录入数据的对象
      }
    });

    // 修改立即用车设置
    $('#optionallyNow-save').click(function () {
      // if(util.checkForm($formVehicle)) {
      var roundsData = [];

      getRoundsData($formOptionallyNow, 'roundsInfo2', roundsData);

      var data = util.formData($formOptionallyNow);
      data.rounds = roundsData;

      data.is_standby = 1;
      data.is_online = 1;

      if ($validateOptionallyNow.form()) {
        util.post(ajaxURL.SCHEDULE + '/v1/schedule/settings/updateOptionallyNow?city_id=' + curCityId, data, function (res) {
          // layer.closeAll();
          util.tip.success('操作成功');
        });
      // } else {
      //   util.tip.warn('请将数据填写完整');
      }
    });

    // subContent3
    //*********************************************************************************************************************/
    var $formSpeciallyBook = $('#subContent3');
    var speciallyBookRounds_now = '';
    var speciallyBookRounds_book = '';

    // 预约接机/站基础设置界面数据填充
    function loadSpeciallyBook(id) {
      util.resetForm($formSpeciallyBook);
      util.ajax(ajaxURL.SCHEDULE + '/v1/schedule/settings/getSpeciallyBook', {
        city_id: id
      }, 'GET', util.resCallback(function (res) {

        // 获取派单轮次数
        speciallyBookRounds_now = res.data.nowRounds.length;
        speciallyBookRounds_book = res.data.bookRounds.length;

        var data = $.extend({}, res.data, {
          nowRounds3: res.data.nowRounds.length,
          bookRounds3: res.data.bookRounds.length,
          nowInterval: res.data.nowRounds[0] && res.data.nowRounds[0].interval,
          bookInterval3: res.data.bookRounds[0] && res.data.bookRounds[0].interval
        })
        util.fillForm($formSpeciallyBook, data);

        // 填充派单轮次数据
        loadRoundsInfo(speciallyBookRounds_now, res.data.nowRounds, 'nowRoundsInfo3');
        loadRoundsInfox(speciallyBookRounds_book, res.data.bookRounds, 'bookRoundsInfo3');
      }
      ));
    }

    // 页面创建派单轮次设置
    $formSpeciallyBook.find("input[name=nowRounds3]").change(function () {
      speciallyBookRounds_now = $(this).val();
      loadRoundsInfo(speciallyBookRounds_now, null, 'nowRoundsInfo3');
    });

    $formSpeciallyBook.find("input[name=bookRounds3]").change(function () {
      speciallyBookRounds_book = $(this).val();
      loadRoundsInfox(speciallyBookRounds_book, null, 'bookRoundsInfo3');
    });

    // 验证表单
    var $validateSpeciallyBook = $formSpeciallyBook.validate({
      rules: {
        notify_time: { required: true, digits: true },
        min_time: { required: true, digits: true },
        max_time: { required: true, digits: true },
        is_send_msg: { required: true },
        msg_id: { required: true, stringCheck: true },
        range1: { required: true, digits: true },
        range2: { required: true, digits: true },
        range3: { required: true, digits: true },
        range4: { required: true, digits: true },
        range5: { required: true, digits: true },
        rangex1: { required: true, digits: true },
        rangex2: { required: true, digits: true },
        rangex3: { required: true, digits: true },
        rangex4: { required: true, digits: true },
        rangex5: { required: true, digits: true },
        is_standby: { required: true },
        is_online: { required: true },
        nowInterval3: { required: true, digits: true },
        bookInterval3: { required: true, digits: true },
        nowRounds3: { required: true, digits: true, max: 5, maxlength: 1 },
        bookRounds3: { required: true, digits: true, max: 5, maxlength: 1 }
      },
      messages: {
        notify_time: { required: '请输入司机信息推送设置', digits: '请输入实数' },
        min_time: { required: '请输入可预约最短时间设置', digits: '请输入实数' },
        max_time: { required: '请输入可预约最长时间设置', digits: '请输入实数' },
        is_send_msg: { required: '请选择是否发短信给乘客' },
        msg_id: { required: '请输入短信模板ID', stringCheck: true },
        range1: { required: '请输入筛选距离', digits: '请输入实数' },
        range2: { required: '请输入筛选距离', digits: '请输入实数' },
        range3: { required: '请输入筛选距离', digits: '请输入实数' },
        range4: { required: '请输入筛选距离', digits: '请输入实数' },
        range5: { required: '请输入筛选距离', digits: '请输入实数' },
        rangex1: { required: '请输入筛选距离', digits: '请输入实数' },
        rangex2: { required: '请输入筛选距离', digits: '请输入实数' },
        rangex3: { required: '请输入筛选距离', digits: '请输入实数' },
        rangex4: { required: '请输入筛选距离', digits: '请输入实数' },
        rangex5: { required: '请输入筛选距离', digits: '请输入实数' },
        is_standby: { required: '请选择是否给待命车辆派车' },
        is_online: { required: '请选择是否给场外车辆派车' },
        nowInterval3: { required: '请输入间隔时间', digits: '请输入实数' },
        bookInterval3: { required: '请输入间隔时间', digits: '请输入实数' },
        nowRounds3: { required: '请输入派单轮次', digits: '请输入整数', max: '请输入小于等于5的数字', maxlength: '长度不超过1' },
        bookRounds3: { required: '请输入派单轮次', digits: '请输入整数', max: '请输入小于等于5的数字', maxlength: '长度不超过1' }
      },
      errorPlacement: function (error, element) { //错误信息位置设置方法
        error.appendTo(element.parents('.form-container')); //这里的element是录入数据的对象
      }
    });

    // 修改预约接机/站设置
    $('#speciallyBook-save').click(function () {
      // if(util.checkForm($formVehicle)) {
      var nowRoundsData = [];
      var bookRoundsData = [];

      getRoundsData($formSpeciallyBook, 'nowRoundsInfo3', nowRoundsData);
      getRoundsData($formSpeciallyBook, 'bookRoundsInfo3', bookRoundsData);

      var data = util.formData($formSpeciallyBook);
      data.nowRounds = nowRoundsData;
      data.bookRounds = bookRoundsData;

      if ($validateSpeciallyBook.form()) {
        var url = ajaxURL.SCHEDULE + '/v1/schedule/settings/updateSpeciallyBook?city_id=' + curCityId;
        util.ajax(url, JSON.stringify(data), 'POST', util.resCallback(function (res) {
          layer.closeAll();
          util.tip.success('操作成功');
        }));
        // }
        // }else {
        //   util.tip.warn('请将数据填写完整');
        // }
      }
    });

    // subContent4
    //*********************************************************************************************************************/
    var $formSpeciallyNow = $('#subContent4');
    var speciallyNowRounds = '';

    // 立即接机/站基础设置界面数据填充
    function loadSpeciallyNow(id) {
      util.resetForm($formSpeciallyNow);
      util.ajax(ajaxURL.SCHEDULE + '/v1/schedule/settings/getSpeciallyNow', {
        city_id: id
      }, 'GET', util.resCallback(function (res) {

        // 获取派单轮次数
        speciallyNowRounds = res.data.rounds.length;

          var data = $.extend({}, res.data, { rounds4: res.data.rounds.length, interval4: res.data.rounds[0] && res.data.rounds[0].interval})
        util.fillForm($formSpeciallyNow, data);

        // 填充派单轮次数据
        loadRoundsInfo(speciallyNowRounds, res.data.rounds, 'roundsInfo4');
      }
      ));
    }

    // 页面创建派单轮次设置
    $formSpeciallyNow.find("input[name=rounds4]").change(function () {
      speciallyNowRounds = $(this).val();
      loadRoundsInfo(speciallyNowRounds, null, 'roundsInfo4');
    });

    // 验证表单
    var $validateSpeciallyNow = $formSpeciallyNow.validate({
      rules: {
        is_standby: { required: true },
        is_online: { required: true },
        is_send_msg: { required: true },
        msg_id: { required: true },
        range1: { required: true, digits: true },
        range2: { required: true, digits: true },
        range3: { required: true, digits: true },
        range4: { required: true, digits: true },
        range5: { required: true, digits: true },
        interval4: { required: true, digits: true },
        rounds4: { required: true, digits: true, max: 5, maxlength: 1 }
      },
      messages: {
        is_standby: { required: '请选择是否给待命车辆派车' },
        is_online: { required: '请选择是否给场外车辆派车' },
        is_send_msg: { required: '请选择是否发短信给乘客' },
        msg_id: { required: '请输入短信模板ID' },
        range1: { required: '请输入筛选距离', digits: '请输入实数' },
        range2: { required: '请输入筛选距离', digits: '请输入实数' },
        range3: { required: '请输入筛选距离', digits: '请输入实数' },
        range4: { required: '请输入筛选距离', digits: '请输入实数' },
        range5: { required: '请输入筛选距离', digits: '请输入实数' },
        interval4: { required: '请输入间隔时间', digits: '请输入实数' },
        rounds4: { required: '请输入派单轮次', digits: '请输入整数', max: '请输入小于等于5的数字', maxlength: '长度不超过1' }
      },
      errorPlacement: function (error, element) { //错误信息位置设置方法
        error.appendTo(element.parents('.form-container')); //这里的element是录入数据的对象
      }
    });

    // 修改立即接机/站设置
    $('#speciallyNow-save').click(function () {
      // if(util.checkForm($formVehicle)) {
      var roundsData = [];

      getRoundsData($formSpeciallyNow, 'roundsInfo4', roundsData);

      var data = util.formData($formSpeciallyNow);
      data.rounds = roundsData;

      if ($validateSpeciallyNow.form()) {
        var url = ajaxURL.SCHEDULE + '/v1/schedule/settings/updateSpeciallyNow?city_id=' + curCityId;
        util.ajax(url, JSON.stringify(data), 'POST', util.resCallback(function (res) {
          layer.closeAll();
          util.tip.success('操作成功');
        }));
        // }
        // }else {
        //   util.tip.warn('请将数据填写完整');
        // }
      }
    });

    // // 选择不给场外车辆派车隐藏派单轮次信息
    // $(document).ready(function () {

    //   $formSpeciallyNow.find("input[name=is_online]").each(function () {
    //     $(this).click(function () {
    //       var isSend = $(this).val();
    //       if (isSend == "1") {
    //         $("#rounds4").css("display", "block");
    //       }
    //       if (isSend == "2") {
    //         $("#rounds4").css("display", "none");
    //       }
    //     });
    //   });

    // });

    // subContent methods
    //*********************************************************************************************************************/

    // 选择不发送短信给乘客隐藏短信模板ID
    $(document).ready(function () {
      hideMsgId($formOptionallyBook);
      hideMsgId($formOptionallyNow);
      hideMsgId($formSpeciallyBook);
      hideMsgId($formSpeciallyNow);
    });

    // 隐藏短信模板ID模块
    function hideMsgId(form) {
      form.find("input[name=is_send_msg]").each(function () {
        $(this).click(function () {
          var isSend = $(this).val();
          if (isSend == "1") {
            form.find("[name=msg_id_hide]").css("display", "block");
          }
          if (isSend == "2") {
            form.find("[name=msg_id_hide]").css("display", "none");
          }
        });
      });
    }

    // 页面渲染后台轮次数据
    function loadRoundsInfo(roundsLength, data, roundsInfo) {
      var ret = '';
      if (roundsLength <= 5) {
        if (data != null) {
          for (var i = 0; i < roundsLength; i++) {
            ret += '<div class="row form-row form-row-sm"><div class="col-xs-4"><div class="form-text_150">第' + (i + 1) + '轮筛选距离：</div><div class="form-container form-inline"><input class="form-control range marginR10" name="range' + (i + 1) + '" value="' + data[i].range * 0.001 + '"><span>公里</span></div></div></div>';
          }
        }
        // 页面操作时传null生成空的数据
        else {
          for (var i = 0; i < roundsLength; i++) {
            ret += '<div class="row form-row form-row-sm"><div class="col-xs-4"><div class="form-text_150">第' + (i + 1) + '轮筛选距离：</div><div class="form-container form-inline"><input class="form-control range marginR10" name="range' + (i + 1) + '"><span>公里</span></div></div></div>';
          }
        }
      }
      else { }
      $('#' + roundsInfo).html(ret);
    }

    // 页面渲染后台轮次数据x
    function loadRoundsInfox(roundsLength, data, roundsInfo) {
      var ret = '';
      if (roundsLength <= 5) {
        if (data != null) {
          for (var i = 0; i < roundsLength; i++) {
            ret += '<div class="row form-row form-row-sm"><div class="col-xs-4"><div class="form-text_150">第' + (i + 1) + '轮筛选距离：</div><div class="form-container form-inline"><input class="form-control range marginR10" name="rangex' + (i + 1) + '" value="' + data[i].range * 0.001 + '"><span>公里</span></div></div></div>';
          }
        }
        // 页面操作时传null生成空的数据
        else {
          for (var i = 0; i < roundsLength; i++) {
            ret += '<div class="row form-row form-row-sm"><div class="col-xs-4"><div class="form-text_150">第' + (i + 1) + '轮筛选距离：</div><div class="form-container form-inline"><input class="form-control range marginR10" name="rangex' + (i + 1) + '"><span>公里</span></div></div></div>';
          }
        }
      }
      else { }
      $('#' + roundsInfo).html(ret);
    }

    // 获取页面派单轮次数据
    function getRoundsData(form, roundsInfo, roundsData) {
      var interval = form.find('.intervax').val()
      form.find('#' + roundsInfo).children().each(function () {
        var $this = $(this);
        roundsData.push({
          range: $this.find('input.range').val() * 1000,
          interval: interval
        })
      })
    }

    //*********************************************************************************************************************/
    // panes
    $('#pane1').click(function () { setTab(1, 5); });
    $('#pane2').click(function () { setTab(2, 5); });
    $('#pane3').click(function () { setTab(3, 5); });
    $('#pane4').click(function () { setTab(4, 5); });
    $('#pane5').click(function () { setTab(5, 5); });

    // subPanes
    $('#subPane1').click(function () { setSubTab(1, 4); });
    $('#subPane2').click(function () { setSubTab(2, 4); });
    $('#subPane3').click(function () { setSubTab(3, 4); });
    $('#subPane4').click(function () { setSubTab(4, 4); });


    function setTab(cursel, n) {
      for (var i = 1; i <= n; i++) {
        var s = "#pane" + i;
        var c = "#content" + i;
        if (i == cursel) {
          $(s).attr('class', 'tab-pane-selected');
          $(c).css("display", "block");
        }
        else {
          $(s).attr('class', 'tab-pane');
          $(c).css("display", "none");
        }
      }
    }

    function setSubTab(cursel, n) {
      for (var i = 1; i <= n; i++) {
        var s = "#subPane" + i;
        var c = "#subContent" + i;
        if (i == cursel) {
          $(s).attr('class', 'tab-pane-selected');
          $(c).css("display", "block");
        }
        else {
          $(s).attr('class', 'tab-pane');
          $(c).css("display", "none");
        }
      }
    }

    // 车辆状态列表
    var $tableStatusList = $('#table-dispatch-statusList');
    function loadDriverTable(id) {
      util.searchGrid($tableStatusList, {
        cityId: id,
        name: '',
        state: ''
       })
      util.resetForm('#form-stasticSearch')
    }

    util.jqGrid($tableStatusList, {
      url: ajaxURL.ORGANIZATION + '/driver/getDriverList?isBind=1',
      //datatype: 'json',
      height: 400,
      width: 1160,
      multiselect: true,
      multiboxonly: true,
      // shrinkToFit: false,
      colModel: [
        { label: '序号', name: 'kid', width: 50 },
        { label: '车牌号', name: 'autoNo', width: 100 },
        {
          label: '车辆类型', name: 'autoId', width: 100, formatter: function (v, o, r) {
            return dictData.type[dictData.auto[v].type] || '';
          }
        },
        {
          label: '品牌', name: 'autoId', width: 80, formatter: function (v, o, r) {
            return dictData.autoBrandId[dictData.auto[v].type] || '';
          }
        },
        {
          label: '颜色', name: 'autoId', width: 80, formatter: function (v, o, r) {
            return dictData.vehicleColor[dictData.auto[v].type] || '';
          }
        },
        { label: '司机名称', name: 'name', width: 120 },
        { label: '司机电话', name: 'phone', width: 120 },
        {
          label: '状态', name: 'state', width: 80, formatter: function (v, o, r) {
            return dictData.state[v] || '';
          }
        },
        {
          label: '当前位置', name: '_curPosition', width: 150
        }
      ],
      pager: '#pager-statusList',
      gridComplete: function() {
        var ids = $tableStatusList.jqGrid('getDataIDs');
        ids.forEach(function (id, index) {
          var rowData = $tableStatusList.jqGrid('getRowData', id)
          getAddressName(rowData.lng, rowData.lat).done(function(data) {
            $tableStatusList.jqGrid('setRowData', id, {
              _curPosition: data
            } )
          })
        });
      }
    });

    // 获取车辆品牌列表
    util.ajax(ajaxURL.ORGANIZATION + '/auto/getAutoBrandList?pageNum=1&pageSize=99999', '', 'GET', util.resCallback(function (res) {
      util.selectHtml($('[name="autoBrandId"]'), res.data, 'kid', 'name', '请选择');
      $.each(res.data, function (_, item) {
        dictData.autoBrandId[item.kid] = item.name;
      });
      // 处理字典
      util.getDict('vehicleColor', 'vehicleColor').then(function (data) {
        util.selectHtml('[name="vehicleColor"]', data, 'value', 'name', '请选择');
        $.each(data, function (_, item) {
          dictData.vehicleColor[item.value] = item.name;
        });
        util.getDict('vehicleType', 'vehicleType').then(function (data) {
          util.selectHtml('[name="type"]', data, 'value', 'name', '请选择');
          $.each(data, function (_, item) {
            dictData.type[item.value] = item.name;
          });
          searchTable();

          $('#btn-driverSearch').click(searchTable)
        });
      });
    }));

    util.getDict('state', 'state').then(function (data) {
      util.selectHtml('[name="state"]', data, 'value', 'name', '请选择');
      $.each(data, function (_, item) {
        dictData.state[item.value] = item.name;
      });
    });

    function searchTable() {
      var data = util.formData('form-stasticSearch');
      util.searchGrid($tableStatusList, data);
    }

    // 获取车辆品牌列表
    util.ajax(ajaxURL.ORGANIZATION + '/auto/getAutoList?pageNum=1&pageSize=99999', '', 'GET', util.resCallback(function (res) {
      $.each(res.data, function (_, item) {
        dictData.auto[item.kid] = item;
      });
    }));

    // 派单失败订单列表
    // var $tableFail = $('#table-dispatch-fail');
    // util.jqGrid($tableFail, {
    //   url: '',
    //   datatype: 'json',
    //   autowidth: true,
    //   height: $(window).height() - 480,
    //   multiselect: true,
    //   multiboxonly: true,
    //   // shrinkToFit: false,
    //   colModel: [
    //     { label: '序号', name: '', width: 80 },
    //     { label: '订单号', name: '', width: 150 },
    //     { label: '乘客电话', name: '', width: 120 },
    //     { label: '订单类型', name: '', width: 80 },
    //     { label: '下单时间', name: '', width: 120 },
    //     { label: '服务时间', name: '', width: 120 },
    //     { label: '出发地', name: '', width: 100 },
    //     { label: '目的地', name: '', width: 100 },
    //     { label: '失败原因', name: '', width: 80 },
    //     { label: '指派车辆', name: '', formatter: function (value, grid, rows, state) { return "<a href=\"#\" style=\"color:#f60\" onclick=\"Modify(" + value + ")\">Edit</a>" } }
    //   ],
    //   pager: '#pager-fail'
    // });



  });


})(window, jQuery);
